home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / Found / FWString / Sources / FWPStr.cpp < prev    next >
Encoding:
Text File  |  1996-04-25  |  14.3 KB  |  420 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWPStr.cpp
  4. //    Release Version:    $ ODF 1 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWFound.hpp"
  11.  
  12. #ifndef FWPSTR_H
  13. #include "FWPStr.h"
  14. #endif
  15.  
  16. #ifndef SLSTRREP_H
  17. #include "SLStrRep.h"
  18. #endif
  19.  
  20. #ifndef FWSTREAM_H
  21. #include "FWStream.h"
  22. #endif
  23.  
  24. #ifndef FWMEMHLP_H
  25. #include "FWMemHlp.h"
  26. #endif
  27.  
  28. #ifndef FWEXCEPT_H
  29. #include "FWExcept.h"
  30. #endif
  31.  
  32. //========================================================================================
  33. // File scope definitions
  34. //========================================================================================
  35.  
  36. #ifdef FW_BUILD_MAC
  37. #pragma segment Strings
  38. #endif
  39.  
  40. //========================================================================================
  41. //    CLASS FW_CString
  42. //========================================================================================
  43.  
  44. FW_DEFINE_AUTO(FW_CString)
  45. FW_DEFINE_CLASS_M0(FW_CString)
  46.  
  47. // This class is archivable, but we provide the archiving implementation in a separate
  48. // translation unit in order to enable deadstripping of the archiving-related code
  49. // in parts that do not use archiving with this class.
  50.  
  51. //----------------------------------------------------------------------------------------
  52. //    FW_CString::~FW_CString
  53. //----------------------------------------------------------------------------------------
  54.  
  55. FW_CString::~FW_CString()
  56. {
  57.     FW_START_DESTRUCTOR
  58.     FW_PrivString_Release(fRep);
  59. }
  60.  
  61. //----------------------------------------------------------------------------------------
  62. //    FW_CString::FW_CString
  63. //----------------------------------------------------------------------------------------
  64.  
  65. FW_CString::FW_CString()
  66.     : fRep(FW_PrivString_AcquireEmptyString())
  67. {
  68.     FW_END_CONSTRUCTOR
  69. }
  70.  
  71. //----------------------------------------------------------------------------------------
  72. //    FW_CString::FW_CString
  73. //----------------------------------------------------------------------------------------
  74.  
  75. FW_CString::FW_CString(ODIText *text)
  76.     : fRep(FW_PrivString_AcquireEmptyString())
  77. {
  78.     ReplaceAll(text);
  79.     FW_END_CONSTRUCTOR
  80. }
  81.  
  82. //----------------------------------------------------------------------------------------
  83. //    FW_CString::FW_CString
  84. //----------------------------------------------------------------------------------------
  85.  
  86. FW_CString::FW_CString(FW_HString rep)
  87.     : fRep(rep)
  88. {
  89.     FW_PrivString_Acquire(fRep);
  90.     FW_END_CONSTRUCTOR
  91. }
  92.  
  93. //----------------------------------------------------------------------------------------
  94. //    FW_CString::FW_CString
  95. //----------------------------------------------------------------------------------------
  96.  
  97. FW_CString::FW_CString(const FW_CString& other)
  98.     : fRep(other.fRep)
  99. {
  100.     FW_PrivString_Acquire(fRep);
  101.     FW_END_CONSTRUCTOR
  102. }
  103.  
  104. //----------------------------------------------------------------------------------------
  105. //    FW_CString::FW_CString
  106. //----------------------------------------------------------------------------------------
  107.  
  108. FW_CString::FW_CString(const char* text)
  109.     : fRep(FW_PrivString_AcquireEmptyString())
  110. {
  111.     FW_PlatformError error = 0;
  112.     fRep = FW_PrivString_ReplaceAllBytes(fRep, text, FW_PrimitiveStringLength(text), &error);
  113.     FW_FailOnError(error);
  114.     FW_END_CONSTRUCTOR
  115. }
  116.  
  117. //----------------------------------------------------------------------------------------
  118. //    FW_CString::FW_CString
  119. //----------------------------------------------------------------------------------------
  120.  
  121. FW_CString::FW_CString(const char* text, FW_ByteCount byteLength)
  122.     : fRep(FW_PrivString_AcquireEmptyString())
  123. {
  124.     FW_PlatformError error = 0;
  125.     fRep = FW_PrivString_ReplaceAllBytes(fRep, text, byteLength, &error);
  126.     FW_FailOnError(error);
  127.     FW_END_CONSTRUCTOR
  128. }
  129.  
  130. //----------------------------------------------------------------------------------------
  131. //    FW_CString::FW_CString
  132. //----------------------------------------------------------------------------------------
  133. FW_CString::FW_CString(const char* text, const FW_Locale& locale)
  134.     : fRep(0)
  135. {
  136.     FW_PlatformError error = 0;
  137.     fRep = FW_PrivString_AcquireEmptyStringWithLocale(locale, &error);
  138.     FW_FailOnError(error);
  139.     fRep = FW_PrivString_ReplaceAllBytes(fRep, text, FW_PrimitiveStringLength(text), &error);
  140.     FW_FailOnError(error);
  141.     FW_END_CONSTRUCTOR
  142. }
  143.  
  144. //----------------------------------------------------------------------------------------
  145. //    FW_CString::FW_CString
  146. //----------------------------------------------------------------------------------------
  147.  
  148. FW_CString::FW_CString(const char* text, FW_ByteCount byteLength, const FW_Locale& locale)
  149.     : fRep(0)
  150. {
  151.     FW_PlatformError error = 0;
  152.     fRep = FW_PrivString_AcquireEmptyStringWithLocale(locale, &error);
  153.     FW_FailOnError(error);
  154.     fRep = FW_PrivString_ReplaceAllBytes(fRep, text, byteLength, &error);
  155.     FW_FailOnError(error);
  156.     FW_END_CONSTRUCTOR
  157. }
  158.  
  159. //----------------------------------------------------------------------------------------
  160. //    FW_CString::FW_CString
  161. //----------------------------------------------------------------------------------------
  162.  
  163. FW_CString::operator FW_HString*()
  164. {
  165.     FW_PlatformError error = 0;
  166.     fRep = FW_PrivString_LockString(fRep, &error);
  167.     FW_FailOnError(error);
  168.     return &fRep;
  169. }
  170.  
  171. //----------------------------------------------------------------------------------------
  172. //    FW_CString::operator[]
  173. //----------------------------------------------------------------------------------------
  174.  
  175. FW_LChar FW_CString::operator[](FW_CharacterPosition position) const
  176. {
  177.     char theChar;
  178. //    FW_PRIV_ASSERT(Not_Yet_Implemented);
  179. //    This implementation will work for now, but it's not correct
  180.     FW_PrivString_Retrieve(fRep, &theChar, sizeof(theChar), position);
  181.     return theChar;
  182. }
  183.  
  184. //----------------------------------------------------------------------------------------
  185. // FW_CString::PrivRead
  186. //----------------------------------------------------------------------------------------
  187.  
  188. FW_CReadableStream& FW_CString::PrivRead(FW_CReadableStream& stream)
  189. {
  190.     FW_Locale locale;
  191.     
  192.     stream >> locale.fScriptCode >> locale.fLangCode;
  193.  
  194.     short byteCount;
  195.     stream >> byteCount;
  196.     
  197.     FW_CAcquireTemporarySystemHandle handle(byteCount);
  198.     char* buffer = (char*) handle.GetPointer();
  199.  
  200.     stream.Read(buffer, byteCount);
  201.     FW_CString temp(buffer, byteCount, locale);
  202.     
  203.     ReplaceAll(temp);
  204.     
  205.     return stream;
  206. }
  207.  
  208. //----------------------------------------------------------------------------------------
  209. // FW_CString::PrivWrite
  210. //----------------------------------------------------------------------------------------
  211.  
  212. FW_CWritableStream& FW_CString::PrivWrite(FW_CWritableStream& stream) const
  213. {
  214.     FW_Locale locale;
  215.     GetLocale(locale);
  216.  
  217.     stream << locale.fScriptCode << locale.fLangCode;
  218.  
  219.     FW_ByteCount longByteCount = GetByteLength();
  220.     FW_ASSERT(longByteCount < 32768L);
  221.     short byteCount = (short) longByteCount;
  222.     stream << byteCount;
  223.     const char* buffer = RevealBuffer();
  224.     stream.Write(buffer, byteCount);
  225.     return stream;
  226. }
  227.  
  228. //----------------------------------------------------------------------------------------
  229. //    FW_CString::GetCharacterLength
  230. //----------------------------------------------------------------------------------------
  231.  
  232. FW_CharacterCount FW_CString::GetCharacterLength() const
  233. {
  234.     FW_PlatformError error = 0;
  235.     FW_CharacterCount result = FW_PrivString_GetCharacterLength(fRep, &error);
  236.     FW_FailOnError(error);
  237.     return result;
  238. }
  239.  
  240. //----------------------------------------------------------------------------------------
  241. //    FW_CString::ParseAsSignedInteger
  242. //----------------------------------------------------------------------------------------
  243.  
  244. long    FW_CString::ParseAsSignedInteger() const
  245. {
  246.     return FW_PrivString_DecimalStringToSignedInteger(fRep);
  247. }
  248.  
  249. //----------------------------------------------------------------------------------------
  250. //    FW_CString::ParseAsUnsignedInteger
  251. //----------------------------------------------------------------------------------------
  252.  
  253. unsigned long    FW_CString::ParseAsUnsignedInteger() const
  254. {
  255.     return FW_PrivString_DecimalStringToUnsignedInteger(fRep);
  256. }
  257.  
  258. //----------------------------------------------------------------------------------------
  259. //    FW_CString::ParseAsHexadecimalInteger
  260. //----------------------------------------------------------------------------------------
  261.  
  262. unsigned long    FW_CString::ParseAsHexadecimalInteger() const
  263. {
  264.     return FW_PrivString_HexadecimalStringToUnsignedInteger(fRep);
  265. }
  266.  
  267. //----------------------------------------------------------------------------------------
  268. //    FW_CString::ParseAsRealNumber
  269. //----------------------------------------------------------------------------------------
  270.  
  271. double    FW_CString::ParseAsRealNumber() const
  272. {
  273.     return FW_PrivString_StringToDouble(fRep);
  274. }
  275.  
  276. //----------------------------------------------------------------------------------------
  277. //    FW_CString::ReplaceAllAsSignedDecimalInteger
  278. //----------------------------------------------------------------------------------------
  279.  
  280. void    FW_CString::ReplaceAllAsSignedDecimalInteger(long integer)
  281. {
  282.     FW_PlatformError error = 0;
  283.     fRep = FW_PrivString_SignedIntegerToDecimalString(fRep, integer, &error);
  284.     FW_FailOnError(error);
  285. }
  286.  
  287. //----------------------------------------------------------------------------------------
  288. //    FW_CString::ReplaceAllAsUnsignedDecimalInteger
  289. //----------------------------------------------------------------------------------------
  290.  
  291. void    FW_CString::ReplaceAllAsUnsignedDecimalInteger(unsigned long integer)
  292. {
  293.     FW_PlatformError error = 0;
  294.     fRep = FW_PrivString_UnsignedIntegerToDecimalString(fRep, integer, &error);
  295.     FW_FailOnError(error);
  296. }
  297.  
  298. //----------------------------------------------------------------------------------------
  299. //    FW_CString::ReplaceAllAsHexadecimalInteger
  300. //----------------------------------------------------------------------------------------
  301.  
  302. void    FW_CString::ReplaceAllAsHexadecimalInteger(unsigned long integer)
  303. {
  304.     FW_PlatformError error = 0;
  305.     fRep = FW_PrivString_UnsignedIntegerToHexadecimalString(fRep, integer, &error);
  306.     FW_FailOnError(error);
  307. }
  308.  
  309. //----------------------------------------------------------------------------------------
  310. //    FW_CString::ReplaceAllAsRealNumber
  311. //----------------------------------------------------------------------------------------
  312.  
  313. void    FW_CString::ReplaceAllAsRealNumber(double real, short fracDigits)
  314. {
  315.     FW_PlatformError error = 0;
  316.     fRep = FW_PrivString_DoubleToString(fRep, real, fracDigits, &error);
  317.     FW_FailOnError(error);
  318. }
  319.     
  320. //========================================================================================
  321. //    CLASS FW_CAcquireNulTerminatedString
  322. //========================================================================================
  323.  
  324. FW_DEFINE_AUTO(FW_CAcquireNulTerminatedString)
  325.  
  326. //----------------------------------------------------------------------------------------
  327. //    FW_CAcquireNulTerminatedString::FW_CAcquireNulTerminatedString
  328. //----------------------------------------------------------------------------------------
  329.  
  330. FW_CAcquireNulTerminatedString::FW_CAcquireNulTerminatedString(const FW_CString& string)
  331.     : fExportedString(NULL)
  332. {
  333.     fExportedString = new char[string.GetByteLength()+1];
  334.     string.ExportCString(fExportedString);
  335.     FW_END_CONSTRUCTOR
  336. }
  337.  
  338. //----------------------------------------------------------------------------------------
  339. //    FW_CAcquireNulTerminatedString::~FW_CAcquireNulTerminatedString
  340. //----------------------------------------------------------------------------------------
  341.  
  342. FW_CAcquireNulTerminatedString::~FW_CAcquireNulTerminatedString()
  343. {
  344.     FW_START_DESTRUCTOR
  345.     delete [] fExportedString;
  346. }
  347.  
  348. //========================================================================================
  349. //    CLASS FW_CAcquireNulTerminatedString255
  350. //========================================================================================
  351.  
  352. //----------------------------------------------------------------------------------------
  353. //    FW_CAcquireNulTerminatedString255::FW_CAcquireNulTerminatedString255
  354. //----------------------------------------------------------------------------------------
  355.  
  356. FW_CAcquireNulTerminatedString255::FW_CAcquireNulTerminatedString255(const FW_CString& string)
  357. {
  358.     FW_PRIV_ASSERT(string.GetByteLength() < sizeof(fExportedString));
  359.     string.ExportCString(fExportedString);
  360. }
  361.  
  362. //========================================================================================
  363. //    Global FW_CString compare functions
  364. //========================================================================================
  365.  
  366. //----------------------------------------------------------------------------------------
  367. //    operator==
  368. //----------------------------------------------------------------------------------------
  369.  
  370. FW_Boolean operator==(const FW_CString &string1, const FW_CString &string2)
  371. {
  372.     return FW_PrivString_Compare(string1.fRep, string2.fRep) == FW_kStringsEqual;
  373. }
  374.  
  375. //----------------------------------------------------------------------------------------
  376. //    operator!=
  377. //----------------------------------------------------------------------------------------
  378.  
  379. FW_Boolean operator!=(const FW_CString &string1, const FW_CString &string2)
  380. {
  381.     return FW_PrivString_Compare(string1.fRep, string2.fRep) != FW_kStringsEqual;
  382. }
  383.  
  384. //----------------------------------------------------------------------------------------
  385. //    operator<
  386. //----------------------------------------------------------------------------------------
  387.  
  388. FW_Boolean operator<(const FW_CString &string1, const FW_CString &string2)
  389. {
  390.     return FW_PrivString_Compare(string1.fRep, string2.fRep) == FW_kStringOneLess;
  391. }
  392.  
  393. //----------------------------------------------------------------------------------------
  394. //    operator>
  395. //----------------------------------------------------------------------------------------
  396.  
  397. FW_Boolean operator>(const FW_CString &string1, const FW_CString &string2)
  398. {
  399.     return FW_PrivString_Compare(string1.fRep, string2.fRep) == FW_kStringOneGreater;
  400. }
  401.  
  402. //----------------------------------------------------------------------------------------
  403. //    operator<=
  404. //----------------------------------------------------------------------------------------
  405.  
  406. FW_Boolean operator<=(const FW_CString &string1, const FW_CString &string2)
  407. {
  408.     return FW_PrivString_Compare(string1.fRep, string2.fRep) != FW_kStringOneGreater;
  409. }
  410.  
  411. //----------------------------------------------------------------------------------------
  412. //    operator>=
  413. //----------------------------------------------------------------------------------------
  414.  
  415. FW_Boolean operator>=(const FW_CString &string1, const FW_CString &string2)
  416. {
  417.     return FW_PrivString_Compare(string1.fRep, string2.fRep) != FW_kStringOneLess;
  418. }
  419.  
  420.